package bancoperu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;



import bancoperu.model.Account;



public class AccountDAO extends BaseDAO {

	
	public Account update(Account vo) throws Exception {
		String query = "UPDATE account SET AccountBalance=? WHERE CustomerID = ? and AccountType=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setDouble(1,vo.getAccountBalance());
			stmt.setString(2, vo.getCustomer().getCustomerId());
			stmt.setInt(3, vo.getAccountType());
		
			
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new Exception(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	


		
		public String GetAccountID(String  CustomerID, int AccountType) throws Exception {
			String query = "SELECT AccountID FROM account WHERE CustomerID=? and AccountType=?";
			Connection con = null;
			PreparedStatement stmt = null;
			String accountid="";
			try {
				ResultSet rs = null;
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setString(1, CustomerID);
				stmt.setInt(2, AccountType);
				
				
				
				
				rs = stmt.executeQuery();
				if (rs.next()) {				
					accountid=rs.getString("AccountID");		

	
				}
				
				
				
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new Exception(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return accountid;
		}
		
		
		
		
		
		
		
		public Account GetAccountBalance2(String customerID, int accountType) throws Exception {
			String query = "SELECT AccountBalance FROM Account WHERE AccountType=? and CustomerID=?";
			Account vo = new Account();
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				con = ConexionBD.obtenerConexion();
				stmt = con.prepareStatement(query);
				stmt.setInt(1, accountType);
				stmt.setString(2, customerID);
				rs = stmt.executeQuery();
				if (rs.next()) {				
					vo.setAccountBalance(rs.getDouble("AccountBalance"));
									
				}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new Exception(e.getMessage());
			} finally {
				this.cerrarResultSet(rs);
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return vo;
	
		}
}